package com.example.robertluozizhao.framecollectdemo.common.manager;

import android.view.View;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * <pre>
 *     author: luozz@anjiu-tech.com
 *     time  : 2020/9/29
 *     desc  :
 * </pre>
 */
public class ProxyManager<T extends View.OnClickListener> {

    private final static ProxyManager mManger = new ProxyManager();

    private ProxyManager() {

    }

    public static ProxyManager getInstance(){
        return mManger;
    }


    public T getListen(T client, int type){
        ClickInvocationHandler handler = new ClickInvocationHandler(client,type);
        T onClickListener = (T) Proxy.newProxyInstance(client.getClass().getClassLoader(),
                client.getClass().getInterfaces(),
                handler);
        return onClickListener;
    }


    static class ClickInvocationHandler<T> implements InvocationHandler {

        private int executeNum;
        private T client;
        private int type;

        public ClickInvocationHandler(T client,int type) {
            this.client = client;
            this.type = type;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            executeNum++;
            System.out.println("代理执行前..  type:"+type);
            method.invoke(client,args);
            System.out.println("代理执行后..executeNum:"+executeNum);

            return null;
        }
    }
}
